<?php
/**
 * User: @lose
 * Date: 2020/10/9
 * Time: 6:56 下午
 */

namespace Frieza\Modules\Photo\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\ObjRelatedBanner as mObjRelatedBanner;
use Doraemon\module\NotePlugin\RelatedBanner\Base;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Illuminate\Support\MessageBag;
use Doraemon\service\ObjRelatedBanner as sObjRelatedBanner;

class RelatedBanner
{
    public function index(Content $content, Request $request)
    {
        $where = $this->buildWhere($request->all());
        $page = $request->input('page', 1);
        $pageSize = $request->input('per_page');
        $limit = [($page - 1) * $pageSize, $pageSize];

        $list = sObjRelatedBanner::getList($where, $limit);
        $gridModel = new pGridDataModel($list['list'], $list['total']);
        $grid = Admin::grid($gridModel, function (Grid $grid) {

            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->equal('related_type', '关联的活动类型')->select(Base::RELATED_DESC);
                    $filter->equal('related_id', '关联的活动id');
                });

                $filter->column(1 / 2, function (Grid\Filter $filter) {
                    $filter->equal('obj_id', 'note_id');
                });
            });

            $grid->disableExport();
            $grid->disableRowSelector();

            $grid->column('obj_id', 'note_id');
            $grid->column('related_type', '关联的活动类型')->display(function () {
                return Base::RELATED_DESC[$this->related_type];
            });
            $grid->column('related_id', '关联的活动id');
            $grid->column('addtime', '添加时间');
            $grid->column('related_status', '状态')->display(function () {
                return mObjRelatedBanner::STATUS_DESC[$this->related_status];
            })->label([
                mObjRelatedBanner::STATUS_NORMAL => 'primary',
            ]);
            $grid->column('admin_id', '最后操作人')->display(function () {
                return Admin::user()->find($this->admin_id)->name;
            });
        });

        return $content->header('活动note')
                    ->breadcrumb(['text' => '活动note'])
                    ->row($grid);
    }

    public function create(Content $content, $id = 0)
    {
        $info = [];
        if($id) {
            $info = mObjRelatedBanner::getInfoById($id);
        }

        $form = new Form($info);
        $form->hidden('id');
        $form->text('obj_id', 'note_id');
        $form->select('related_type', '关联的活动类型')->options(Base::RELATED_DESC);
        $form->text('related_id', '关联的活动id');
        $form->action('/admin/photo/relatedBanner/save');

        return $content->header('活动note')
                ->breadcrumb(['text' => '活动note', 'url' => 'photo/relatedBanner'], ['text' => '编辑活动note'])
                ->row($form);
    }

    public function save(Request $request)
    {
        $id = $request->input('id', 0);
        $data = [
            'obj_id' => $request->input('obj_id', ''),
            'related_type' => $request->input('related_type', ''),
            'related_id' => $request->input('related_id', ''),
            'related_status' => mObjRelatedBanner::STATUS_NORMAL,
            'admin_id' => Admin::user()->id,
        ];

        $id = sObjRelatedBanner::save($id, $data);
        if($id) {
            return redirect(url("/admin/photo/relatedBanner"));
        } else {
            $error = new MessageBag([
                'title' => '保存失败，请重试'
            ]);
            return back()->with(compact('error'))->withInput();
        }
    }

    public static function delete($id)
    {
        $id = sObjRelatedBanner::delete($id, Admin::user()->id);
        if($id) {
            return response()->json([
                'status' => true,
                'message' => "删除成功",
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => '删除失败',
            ]);
        }
    }

    private function buildWhere($params)
    {
        $where = [];
        $where['related_status'] = mObjRelatedBanner::STATUS_NORMAL;
        if(isset($params['obj_id']) && $params['obj_id']) {
            $where['obj_id'] = $params['obj_id'];
        }

        if(isset($params['related_id']) && $params['related_id']) {
            $where['related_id'] = $params['related_id'];
        }

        if(isset($params['related_type']) && $params['related_type']) {
            $where['related_type'] = $params['related_type'];
        }

        return $where;
    }
}